//
//  ViewController.m
//  09-UITableViewEdit
//
//  Created by dllo on 16/7/26.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"

#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.width
#define KTableViewReuse @"cellReuse"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)NSMutableArray *arr;
@property (nonatomic, strong)NSMutableArray<NSIndexPath *> *selectIndexArr;
// 选中名字的数组
@property (nonatomic, strong)NSMutableArray<NSString *>*selectNameArr;
@property (nonatomic, strong)UITableView *tableView;
@end
@implementation ViewController
- (NSMutableArray *)arr {

    if (!_arr) {
        self.arr = [NSMutableArray array];
    }
    return _arr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.selectIndexArr = [NSMutableArray array];
    self.selectNameArr = [NSMutableArray array];
    NSArray *array = @[@"唐益梦", @"张合尉", @"高雅馨", @"符淋淋", @"孙遥", @"李毅然", @"于瀚", @"沙长健", @"卢思彤", @"徐昊", @"曹冠雄", @"陈丹明", @"罗浩", @"肖扬", @"王道林", @"宫雪", @"李成", @"冀永金", @"姜浩", @"罗鹏飞", @"李昊林", @"郭宝", @"马峰", @"刘坦奇", @"殷明静", @"邓凯", @"徐康", @"王鍠", @"韩旭", @"杨睿", @"张信宝", @"姜程程", @"包铭礼", @"杜江", @"苗钧豪"];
    self.arr = [NSMutableArray arrayWithArray:array];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:KTableViewReuse];
    // tableView编辑
    // 1.设置右barButtonItem为编辑的editButtonItem
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    NSLog(@"%@", NSStringFromSelector(self.editButtonItem.action));
    self.editButtonItem.action = @selector(editAction);
}
- (void)editAction {
    [self performSelector:NSSelectorFromString(@"_toggleEditing:")];
    if (self.editing == NO) {
        // 删除数据源
        [_arr removeObjectsInArray:_selectNameArr];
        // 要删除选中的数组
        [_tableView deleteRowsAtIndexPaths:_selectIndexArr withRowAnimation:UITableViewRowAnimationLeft];
        [_selectNameArr removeAllObjects];
        [_selectIndexArr removeAllObjects];
    }
}
#pragma mark - delegate
// 4. 返回cell的编辑状态(样式)
// 没有 UITableViewCellEditingStyleNone
// 默认 UITableViewCellEditingStyleDelete
// 添加 UITableViewCellEditingStyleInsert
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    //return UITableViewCellEditingStyleInsert;
#warning mark - 多选
    return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}
// 选中状态
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        // 取出名字
        [_selectNameArr addObject:_arr[indexPath.row]];
        // 在全选状态下, 数组中添加对应选中的下标
        [_selectIndexArr addObject:indexPath];
    }
}
// 取消选中状态
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editing) {
        [_selectNameArr removeObject:_arr[indexPath.row]];
        // 在全选状态下, 数组中移除对应选中的下标
        [_selectIndexArr removeObject:indexPath];
    }
}
#pragma mark - tableDataSource
// Edit
// command + option + [
    // 2.让tableView进入编辑状态(VC方法)
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    if (editing) {
        NSLog(@"编辑");
    } else {
        // 删除数据源
        [_arr removeObjectsInArray:_selectNameArr];
        // 要删除选中的数组
        [_tableView deleteRowsAtIndexPaths:_selectIndexArr withRowAnimation:UITableViewRowAnimationLeft];
        [_selectNameArr removeAllObjects];
        [_selectIndexArr removeAllObjects];
        NSLog(@"完成");
    }
}
    // 3.控制tableView能否编辑 YES可以 NO 不能编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
    // 5.提交编辑状态(对应按钮的点击状态)
    // 滑动删除, 只和这个方法有关系
#warning 如果对tableView进行添加/删除,先修改数据源, 再去进行对应的添加/删除
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 1.修改数据源
        [_arr removeObjectAtIndex:indexPath.row];
        //对应修改数据
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        [_arr insertObject:@"@.@" atIndex:indexPath.row];
        [_tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
    // tableView不论 删除还是添加  都已经进行了刷新操作
}
#warning mark - 移动
// Move 移动
// 1.DataSore 能否移动, 默认YES
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
// 2.移动完成更改数据源
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(nonnull NSIndexPath *)sourceIndexPath toIndexPath:(nonnull NSIndexPath *)destinationIndexPath {
    [_arr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KTableViewReuse forIndexPath:indexPath];
    cell.textLabel.text = _arr[indexPath.row];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
